package com.timothy.android.view;

import java.util.List;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import com.timothy.android.util.NumberUtil;
import com.timothy.answer.ChoiceResult;
import com.timothy.chart.CanvasSetting;
import com.timothy.config.Configuration;
import com.timothy.statistics.AnswerBuilder;

public class Pie3DView extends BaseChartView{
	private final static String LOG_TAG = "Pie2DView";
	
	private final static int THICKNESS = 10;
	
	public Pie3DView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public void drawChart(SurfaceHolder sfh,BaseBean baseBean,Integer page,Integer screenSize ){
		Log.i(LOG_TAG, "DrawVertical2DBar()...");
		Log.i(LOG_TAG, "page:"+page);
		
		Canvas canvas = sfh.lockCanvas();
		int canvasHeight = canvas.getHeight();
		int canvasWidth = canvas.getWidth();
		Log.i(LOG_TAG, "Canvas Height:"+canvasHeight);
		Log.i(LOG_TAG, "Canvas Width:"+canvasWidth);
		
		CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,screenSize);
		
		int leftM = setting.getLeftMargin();
		int rightM = setting.getRightMargin();
		int topM = setting.getTopMargin();
		int bottomM = setting.getBottomMargin();
		
		Log.i(LOG_TAG, "VerticalPie2DView:leftM:"+leftM);
		Log.i(LOG_TAG, "VerticalPie2DView:rightM:"+rightM);
		Log.i(LOG_TAG, "VerticalPie2DView:topM:"+topM);
		Log.i(LOG_TAG, "VerticalPie2DView:bottomM:"+bottomM);
		
		canvas.drawColor(setting.getBgColor());// 设置画布背景为黑色
		
		//write question
		Paint mPaint = new Paint();
		mPaint.setColor(Color.parseColor(CanvasSetting.CHART_QUESTION_TEXT_COLOR));		
		mPaint.setTextSize(setting.getQueTextSize());
		int queX = setting.getLeftMargin();
		int queY = setting.getTopMargin();
		Log.i(LOG_TAG, "VerticalBar2DView:queX:"+queX);
		Log.i(LOG_TAG, "VerticalBar2DView:queY:"+queY);
		String questionString = baseBean.getQuestionText();
		int queLength = questionString.length();
		int line = 0;
		int lineHeight = 0;
		if(queLength>CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL){
			Log.i(LOG_TAG, "VerticalBar2DView:draw question text->queLength>"+CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL);
			for(line = 0;line<queLength/CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL;line++){
				canvas.drawText(questionString.substring(CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL*line,CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL*(line+1)),queX,queY/4+lineHeight,mPaint);
				Log.i(LOG_TAG, "VerticalBar2DView:start:"+CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL*line+";end:"+CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL*(line+1));
				lineHeight+= queY/4;
			}
			Log.i(LOG_TAG, "VerticalBar2DView:start:"+CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL*line);
			canvas.drawText(questionString.substring(line*CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL),queX,queY/4+lineHeight,mPaint);
		}else{
			Log.i(LOG_TAG, "VerticalBar2DView:draw question text->queLength<="+CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL);
			canvas.drawText(questionString,queX,queY/2,mPaint);
		}
		canvas.save();
		
		int pieWidth = canvasWidth - (int)(setting.getLeftMargin()*1.1 + setting.getRightMargin()*1.1);
		int pieHeight = pieWidth/2;

		int startX = (int)(setting.getLeftMargin()*1.1);
		int startY = (int)(setting.getTopMargin()*1.1);
		int endX = startX + pieWidth;
		int endY = startY + pieHeight;
		
//		PageBaseBean pageBean = baseBean.getCurrentPageBaseBean(page);
//		List<ChoiceResult> pageList = pageBean.getCrList();
//		int pageListSize = pageList.size();
//		if(pageListSize==0) return;
		
	    List<ChoiceResult> totalChoiceResults = baseBean.getAllChoiceResultList();
	    int totalSize = totalChoiceResults.size();
	    if(totalSize==0) return;
		
		for(int j=0;j<=THICKNESS;j++){
			float startAngle = 0;
			for(int i=0;i<totalSize;i++){
				ChoiceResult cr = totalChoiceResults.get(i);
				mPaint.setColor(Color.parseColor(Configuration.SHADOW_COLOR_ARR[i%AnswerBuilder.CHOICE_COUNT_PER_PAGE]));
				RectF recF = new RectF(startX,startY-j,endX,endY-j);
				float sweepAngle = new Float(cr.getShare()*360) ;
				canvas.drawArc(recF, startAngle, sweepAngle, true, mPaint);
				startAngle = startAngle + sweepAngle;
				canvas.save();
			}
			
			if(j==THICKNESS){
				startAngle = 0;
				for(int i=0;i<totalSize;i++){
					ChoiceResult cr = totalChoiceResults.get(i);
					mPaint.setColor(Color.parseColor(Configuration.CHART_COLOR_ARR[i%AnswerBuilder.CHOICE_COUNT_PER_PAGE]));
					
					RectF recF = new RectF(startX,startY-THICKNESS,endX,endY-THICKNESS);
					
					float sweepAngle = new Float(cr.getShare()*360) ;
					canvas.drawArc(recF, startAngle, sweepAngle, true, mPaint);
					startAngle = startAngle + sweepAngle;
					canvas.save();
					
					int textStartY = (canvasHeight/10)* 4; 
					int textEndY = canvasHeight - setting.getBottomMargin();
					int textHeight = (textEndY - textStartY)/10;
					
					int textX = startX; 
					int textY = textStartY+(i+1)*textHeight;
					
					Log.i(LOG_TAG, "textStartY:" + textStartY);
					Log.i(LOG_TAG, "textEndY:" + textEndY);
					Log.i(LOG_TAG, "textHeight:" + textHeight);
					
					Log.i(LOG_TAG, "textX:" + textX);
					Log.i(LOG_TAG, "textY:" + textY);
					
					//draw choice
					mPaint.setTextSize(setting.getChoiceTextSize());
					mPaint.setColor(Color.parseColor(Configuration.CHART_CHOICE_TEXT_COLOR));
					canvas.drawText(String.valueOf(cr.getIndex()), textX , textY, mPaint);
					
					//draw votes and share
					mPaint.setTextSize(setting.getAnswerTextSize());
					mPaint.setColor(Color.parseColor(Configuration.CHART_ANSWER_TEXT_COLOR));
					
					//answerTextStyle = 1 show votes;
					//answerTextStyle = 2 show share;
					//answerTextStyle = 3 show votes and share;
					int votes = cr.getCount();
					double votesShare = cr.getShare();
					String votesStr = String.valueOf(votes);
					String shareStr = "0%";
					if(votesShare*100>0){
						shareStr = NumberUtil.formatData0Deci(votesShare*100)+"%";
					}
					int answerTextStyle = baseBean.getAnswerTextStyle(); 
					switch (answerTextStyle){
					case 1:canvas.drawText(votesStr, textX + setting.getLeftMargin() , textY, mPaint);break;
					case 2:canvas.drawText(shareStr, textX + setting.getLeftMargin()*2, textY, mPaint);break;
					case 3:
						canvas.drawText(votesStr, textX + setting.getLeftMargin() ,textY, mPaint);
						canvas.drawText(shareStr, textX + setting.getLeftMargin()*2, textY, mPaint);
					}
					canvas.save();
				}
			}
			

		}

		//draw border
//		mPaint.setColor(Color.WHITE);
//		for(int x=1;x<10;x++){
//			//draw horizontal lines
//			canvas.drawLine(leftM,x* canvasHeight/10, canvasWidth-rightM,x*canvasHeight/10, mPaint);
//		}
//		for(int y=1;y<10;y++){
//			//draw vertical lines
//			canvas.drawLine(y* canvasWidth/10,topM, y* canvasWidth/10, canvasHeight-bottomM, mPaint);
//		}
		
		canvas.restore();
		mHolder.unlockCanvasAndPost(canvas);
	}

	
}
